#include "DynamicArray.h"
#include <stdio.h>
#include <string.h>

struct Student      //结构体
{
    int id;         //学号
    char* name;     //姓名
};
typedef struct Student Stu;

Stu* CreateStu(int id,const char *name)
{
    Stu*s = (Stu*)malloc(sizeof(Stu));
    if(s == NULL)
    {
        printf("CreateStu malloc error!\n");
        exit(-1);
    }
    s->id = id;
    s->name = strdup(name);
    return s;
}

void ClearStu(void *p)
{
    Stu*s = (Stu*)p;
    free(s->name);
    free(s);
    //printf("清理学生!\n");
}

//回调函数
void PrintStu(void *p)
{
    Stu *s = (Stu*)p;
    printf("学生学号:%d  学生姓名:%s\n",s->id,s->name);
}

bool IsNameEqual(void *p1,void *p2)
{
    Stu *s = (Stu*)p1;
    const char *name = (const char*)p2;
    return strcmp(s->name,name) == 0;
}

bool IsIDEqual(void *p1,void *p2)
{
    Stu *s = (Stu *)p1;
    long id = (long)p2;
    return s->id == id;
}

void PrintClass(void *p)
{
    DArray *a = (DArray *)p;
    printf("班级:\n");
    PrintDArray(a,PrintStu);
}

void ClearClass(void *p)
{
    DArray *a = (DArray *)p;
    // printf("班级:\n");
    ClearDArray(a,PrintStu);
}

void Split(DArray *a,const char *src,const char *delim)
{
    //char temp[] = src;          //在栈上
    char *temp = strdup(src);    //在堆上
    char *dest = strtok(temp,delim);
    while( dest != NULL)
    {
        InsertDArray(a,strdup(dest));
        dest = strtok(NULL,delim);
    }
    //free(temp);
}

void PrintString(void *p)
{
    printf("%s\n",(char *)p);
}

bool BiggerID(void *p1,void *p2)
{
    Stu *s1 = (Stu*)p1;
    Stu *s2 = (Stu *)p2;
    return s1->id > s2->id;
}

bool BiggerName(void *p1,void *p2)
{
    Stu *s1 = (Stu*)p1;
    Stu *s2 = (Stu *)p2;
    return strcmp(s1->name,s2->name) > 0;
}

bool SmallerName(void *p1,void *p2)
{
    Stu *s1 = (Stu*)p1;
    Stu *s2 = (Stu *)p2;
    return strcmp(s1->name,s2->name) < 0;
}

int IDcmp(void *p1,void *p2)
{
    Stu *s1 = (Stu*)p1;
    Stu *s2 = (Stu *)p2;
    return s1->id - (long)p2;
}

int Namecmp(void *p1,void *p2)
{
    Stu *s1 = (Stu *)p1;
    return strcmp(s1->name,(const char *)p2);
}

int main()
{
    // DArray* a = InitDArray();
    // // for(int i = 0; i <= 30; i++)
    // // {
    // //     InsertDArray(a,i);
    // // }
    // InsertDArray(a, CreateStu(1,"张三"));
    // InsertDArray(a, CreateStu(2,"李四"));
    // InsertDArray(a, CreateStu(3,"王五"));

    // void *s = FindByElement(a,"李1四",IsNameEqual);
    // void *s = FindByElement(a,(void *)3,IsIDEqual);
    // void *s = RemoveDArray(a,(void *)3,IsIDEqual);
    // void *s = RemoveDArray(a,"李四",IsNameEqual);
    // if(s)
    // {
    //     printf("删除的学生是:\n");
    //     PrintStu(s);
    //     ClearStu(s);
    // }
    // else
    // {
    //     printf("没找到,没删除!\n");
    // }
    // //PrintDArray(a,PrintStu);
    // // ClearDArray(a,ClearStu);

    // DArray *class1 = InitDArray();
    // InsertDArray(class1, CreateStu(1, "张三"));
    // InsertDArray(class1, CreateStu(2, "李四"));
    // InsertDArray(class1, CreateStu(3, "王五"));

    // DArray *class2 = InitDArray();
    // InsertDArray(class2, CreateStu(4, "张三"));
    // InsertDArray(class2, CreateStu(5, "李七"));
    // InsertDArray(class2, CreateStu(6, "王八"));
    // InsertDArray(class2, CreateStu(7, "王八"));

    // DArray *school = InitDArray();
    // InsertDArray(school, class1);
    // InsertDArray(school, class2);

    // PrintDArray(school,PrintClass);
    // ClearDArray(school,ClearClass);

    //字符串切割    字符串列表
    // DArray *a = InitDArray();
    // Split(a,"zhangsan,lisi,wangwu,wangliu,wangqi,wangba,wangjiu",",");
    // //打印字符串数组
    // PrintDArray(a,PrintString);
    // ClearDArray(a,free);

    DArray *a = InitDArray();
    InsertDArray(a,CreateStu(3,"zhangsan"));
    InsertDArray(a,CreateStu(1,"lisi"));
    InsertDArray(a,CreateStu(2,"wangwu"));

    // FastSort(a,BiggerID);
    // PrintDArray(a,PrintStu);

    // FastSort(a,BiggerName);
    // PrintDArray(a,PrintStu);

    // FastSort(a,SmallerName);
    // PrintDArray(a,PrintStu);

    FastSort(a,BiggerID);
    //void *s = BinarySearch(a,"lisi",Namecmp);
    void *s = BinarySearch(a,(void*)3,IDcmp);
    if(s)
    {
        printf("找到了：");
        PrintStu(s);
    }
    else
    {
        printf("没找到！\n");
    }
    // PrintDArray(a,PrintStu);
    return 0;
}

